//
//  RequestFinacialReimbursementViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/20.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
class RequestFinacialReimbursementViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource,ArrayHandle,TZImagePickerControllerDelegate,UITextFieldDelegate,FetchResult {
    
    var pickerpage = 1
    
    let pagesize = 20
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if (textField.keyboardType == .decimalPad) {
            if textField.text?.count == 0 {
                if string == "." {
                    return false
                }
            } else {
                let unit = textField.text! + string
                let reg = "^[0-9]+(.[0-9]{0,2})?$"
                let predicate = NSPredicate.init(format: "SELF MATCHES %@", reg)
                let result = predicate.evaluate(with: unit)
                return result
            }
        }
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if (textField.keyboardType == .decimalPad) {
            if let laststr = textField.text?.last {
                if laststr == "." {
                    textField.text =  textField.text?.trimmingCharacters(in: CharacterSet.init(charactersIn: "."))
                }
            }
            self.tableView.beginUpdates()
            let row = IndexPath.init(row: 2, section: 1 + details.count)
            let row1 = IndexPath.init(row: 3, section: 1 + details.count)
            self.tableView.reloadRows(at: [row1,row], with: .none)
            self.tableView.endUpdates()
        }
    }
    
    func handleArray(array: Array<JSON>) {
        parterners = array
        tableView.beginUpdates()
        tableView.reloadRows(at: [IndexPath.init(row: 1, section: 0),IndexPath.init(row: 2, section: 0)], with: .none)
        tableView.endUpdates()
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if projectorvehicleordb.0 == true && projectorvehicleordb.1 == false && projectorvehicleordb.2 == false {
            return projects.count
        } else if (projectorvehicleordb.0 == false && projectorvehicleordb.1 == true && projectorvehicleordb.2 == false) {
            return vechcles.count
        } else if (projectorvehicleordb.0 == false && projectorvehicleordb.1 == false && projectorvehicleordb.2 == true) {
            return 2
        }
        return 0
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel()
        label.textColor = UIColor.hex("333333")
        label.font = UIFont.systemFont(ofSize: 15)
        label.textAlignment = .center
        if projectorvehicleordb.0 == true && projectorvehicleordb.1 == false && projectorvehicleordb.2 == false {
            let text = projects[row].contents?["gsProjName"]?.rawString()
            label.text = text
        } else if (projectorvehicleordb.0 == false && projectorvehicleordb.1 == true && projectorvehicleordb.2 == false) {
            let text = vechcles[row].contents?["name"]?.rawString()
            label.text = text
        } else if (projectorvehicleordb.0 == false && projectorvehicleordb.1 == false && projectorvehicleordb.2 == true) {
            if row == 0  {
                label.text = "否"
            } else {
                label.text = "是"
            }
        }
        return label
        
    }
    
    override var names: Array<String> {
        get {
            return ["Projects","Vechcles","Submit","UPLOAD","GETSERIALNUMBER"]
        }
    }
    
    override func path(for name: String) -> String {
        if name == "Projects" {
            return ReimbursementProjectList
        } else if name == "Vechcles" {
            return Transportype
        } else if name == "Submit" {
            return RequestFinacialReimburse
        } else if name == "TogetherMans" {
            return Parterners
        } else if name == "UPLOAD" {
            return UPLOADPATH
        } else if name == "GETSERIALNUMBER" {
            return "/financesAccesory/mobile/business/travelReimbursement/getNumber"
        }
        return ""
    }
    
    override func strategy(for name: String) -> (Bool, String?, (() -> Void)?) {
        
        switch name {
        case "Submit":
            if selectedProject == nil {
                return (false,"请选择项目名称",nil)
            }
            
            if (selectedProject == nil) {
                return (false,"请选择一个项目",nil)
            }
            
            guard let cell = cellForRow(at: IndexPath(row: 4, section: 0), identifier: "InputTableViewCell") as? InputTableViewCell else {
                return (false,"请输入出差天数",nil)
            }
            
            if cell.contentLabel.text?.count == 0 {
                return (false,"请输入出差天数",nil)
            }
            
            guard let cell1 = cellForRow(at: IndexPath.init(row: 4, section: 0), identifier: "InputTableViewCell") as? InputTableViewCell else {
                return (false,"请输入出差事由",nil)
            }
            
            if cell1.contentLabel.text?.count == 0 {
                return (false,"请输入出差事由",nil)
            }
            for section in 1 ..< 1 + details.count {
                let indexpath = IndexPath.init(row: 0, section: section)
                guard let cell0 = cellForRow(at: indexpath, identifier: "UITableViewCell") else {
            
                    return (false,"请选择出发日期",nil)
                }
                if cell0.detailTextLabel?.text == "请选择" {
                    return (false,"请选择出发日期",nil)
                }
                let indexpath1 = IndexPath.init(row: 1, section: section)
                guard let cell1 = cellForRow(at: indexpath1, identifier: "UITableViewCell") else {
                    return (false,"请选择到达日期日期",nil)
                }
                if cell1.detailTextLabel?.text == "请选择" {
                    return (false,"请选择到达日期日期",nil)
                }
                let indexpath2 = IndexPath.init(row: 2, section: section)
                guard let cell2 = cellForRow(at: indexpath2, identifier: "UITableViewCell") as? InputTableViewCell else {
                    return (false,"请输入出发地点",nil)
                }
                if cell2.contentLabel.text?.count == 0 {
                    return (false,"请输入出发地点",nil)
                }
                let indexpath3 = IndexPath.init(row: 3, section: section)
                guard let cell3 = cellForRow(at: indexpath3, identifier: "InputTableViewCell") as? InputTableViewCell else {
                    return (false,"请输入到达点",nil)
                }
                if cell3.contentLabel.text?.count == 0 {
                    return (false,"请输入到达地点",nil)
                }
                
                let indexpath4 = IndexPath.init(row: 4, section: section)
                
                let indexpath5 = IndexPath.init(row: 5, section: section)
                guard let cell5 = cellForRow(at: indexpath5, identifier: "UITableViewCell") as? InputTableViewCell else {
                    return (false,"请输入交通费",nil)
                }
                if cell5.contentLabel.text?.count == 0 {
                    guard let cell4 = cellForRow(at: indexpath4, identifier: "UITableViewCell") else {
                        return (false,"请选择交通方式",nil)
                    }
                    let way = cell4.detailTextLabel?.text
                    if way != "请选择" {
                        return (false,"请输入交通费",nil)
                    }
                } else {
                    guard let cell4 = cellForRow(at: indexpath4, identifier: "UITableViewCell") else {
                        return (false,"请选择交通方式",nil)
                    }
                    
                    if cell4.detailTextLabel?.text?.count == 0 || cell4.detailTextLabel?.text == "请选择" {
                        return (false,"请选择交通方式",nil)
                    }
                }
                
//                let indexpath6 = IndexPath.init(row: 6, section: section)
//                guard let cell6 = cellForRow(at: indexpath6, identifier: "InputTableViewCell") as? InputTableViewCell else {
//                    return (false,"请输入住宿费",nil)
//                }
//                if cell6.contentLabel.text?.count == 0 {
//                    return (false,"请输入住宿费",nil)
//                }
                let indexpath7 = IndexPath.init(row: 7, section: section)
                guard let cell7 = cellForRow(at: indexpath7, identifier: "UITableViewCell") else {
                    return (false,"请选择是否达标",nil)
                }
                if cell7.detailTextLabel?.text?.count == 0 {
                    return (false,"请选择是否达标",nil)
                }
            }
//            for i in  [0,1] {
//                guard let cell = cellForRow(at: IndexPath.init(row: i, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell else {
//                    return (false, "请输入补助费用",nil)
//                }
//                if cell.contentLabel.text?.count == 0 {
//                    return (false, "请输入补助费用",nil)
//                }
//            }
            return (true,nil,nil)
        case "UPLOAD":
            if selectedProject == nil {
                return (false,"请选择项目名称",nil)
            }
            
            if (selectedProject == nil) {
                return (false,"请选择一个项目",nil)
            }
            
            guard let cell = cellForRow(at: IndexPath(row: 3, section: 0), identifier: "InputTableViewCell") as? InputTableViewCell else {
                return (false,"请输入出差天数",nil)
            }
            
            if cell.contentLabel.text?.count == 0 {
                return (false,"请输入出差天数",nil)
            }
            
            guard let cell1 = cellForRow(at: IndexPath.init(row: 4, section: 0), identifier: "InputTableViewCell") as? InputTableViewCell else {
                return (false,"请输入请假事由",nil)
            }
            
            if cell1.contentLabel.text?.count == 0 {
                return (false,"请输入请假事由",nil)
            }
            for section in 1 ..< 1 + details.count {
                let indexpath = IndexPath.init(row: 0, section: section)
                guard let cell0 = cellForRow(at: indexpath, identifier: "UITableViewCell") else {
                    return (false,"请选择出发日期",nil)
                }
                if cell0.detailTextLabel?.text == "请选择" {
                    return (false,"请选择出发日期",nil)
                }
                let indexpath1 = IndexPath.init(row: 1, section: section)
                guard let cell1 = cellForRow(at: indexpath1, identifier: "UITableViewCell") else {
                    return (false,"请选择到达日期日期",nil)
                }
                if cell1.detailTextLabel?.text == "请选择" {
                    return (false,"请选择到达日期日期",nil)
                }
                let indexpath2 = IndexPath.init(row: 2, section: section)
                guard let cell2 = cellForRow(at: indexpath2, identifier: "UITableViewCell") as? InputTableViewCell else {
                    return (false,"请输入出发地点",nil)
                }
                if cell2.contentLabel.text?.count == 0 {
                    return (false,"请输入出发地点",nil)
                }
                let indexpath3 = IndexPath.init(row: 3, section: section)
                guard let cell3 = cellForRow(at: indexpath3, identifier: "InputTableViewCell") as? InputTableViewCell else {
                    return (false,"请输入到达点",nil)
                }
                if cell3.contentLabel.text?.count == 0 {
                    return (false,"请输入到达地点",nil)
                }
                
                let indexpath4 = IndexPath.init(row: 4, section: section)
                
                let indexpath5 = IndexPath.init(row: 5, section: section)
                guard let cell5 = cellForRow(at: indexpath5, identifier: "UITableViewCell") as? InputTableViewCell else {
                    return (false,"请输入交通费",nil)
                }
                if cell5.contentLabel.text?.count == 0 {
                    guard let cell4 = cellForRow(at: indexpath4, identifier: "UITableViewCell") else {
                        return (false,"请选择交通方式",nil)
                    }
                    let way = cell4.detailTextLabel?.text
                    if way != "请选择" {
                        return (false,"请输入交通费",nil)
                    }
                } else {
                    guard let cell4 = cellForRow(at: indexpath4, identifier: "UITableViewCell") else {
                        return (false,"请选择交通方式",nil)
                    }
                    
                    if cell4.detailTextLabel?.text?.count == 0 || cell4.detailTextLabel?.text == "请选择" {
                        return (false,"请选择交通方式",nil)
                    }
                }
                
//                let indexpath6 = IndexPath.init(row: 6, section: section)
//                guard let cell6 = cellForRow(at: indexpath6, identifier: "InputTableViewCell") as? InputTableViewCell else {
//                    return (false,"请输入住宿费",nil)
//                }
//                if cell6.contentLabel.text?.count == 0 {
//                    return (false,"请输入住宿费",nil)
//                }
                let indexpath7 = IndexPath.init(row: 7, section: section)
                guard let cell7 = cellForRow(at: indexpath7, identifier: "UITableViewCell") else {
                    return (false,"请选择是否达标",nil)
                }
                if cell7.detailTextLabel?.text?.count == 0 {
                    return (false,"请选择是否达标",nil)
                }
            }
//            for i in  [0,1] {
//                guard let cell = cellForRow(at: IndexPath.init(row: i, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell else {
//                    return (false, "请输入补助费用",nil)
//                }
//                if cell.contentLabel.text?.count == 0 {
//                    return (false, "请输入补助费用",nil)
//                }
//            }
            return (true,nil,nil)
        case "TogetherMans":
            if (selectedProject == nil) {
                return (false,"请选择一个项目",nil)
            }
            return (true,nil,nil)
        default:
            return (true,nil,nil)
        }
    }
    
    var uploadfilespath: [Dictionary<String,String>] = []
    
    var serialNumber: String = ""
    
    override func param(for name: String) -> Dictionary<String, Any> {
        switch name {
        case "Submit":
            var detaildic: Dictionary<String,String> = [:]
            for i in 0 ..< uploaddetails.count {
                let item = uploaddetails[i]
                for key in item.keys {
                    let destkey = "reimbursementDetails[" + String(i) + "]" + "." + key
                    detaildic[destkey] = item[key]
                }
            }
            var uploaddic: Dictionary<String,String> = [:]
            let orgCode = selectedProject!["orgCode"].rawString()!
            let name = User.name
            for i in 0 ..< uploadfilespath.count {
                let item = uploadfilespath[i]
                for key in item.keys {
                    let destkey = "attachFiles[" + String(i) + "]" + "." + key
                    uploaddic[destkey] = item[key]
                }
            }
            let fyhjd = (Double.init(self.fyhj) ?? 0.0) * 100
            let fyhjint = Int(fyhjd)
            let param = ["draftStatus": "false","projectName":projectName,"projectId":projectId,"attachmentNum":String(files.count),"peerStaff":membersid,"peerStaffName":members,"daysOfTravel": days,"businessTrip": reasons,"travelAllowance": jtbz,"totalCost": fyhjint,"foodAllowance":hsbz,"reamake":remarks,"applyHuman":name,"orgCode":orgCode,"serialNumber":serialNumber] + detaildic + uploaddic
            
            return param
        case "TogetherMans":
            return ["projectId":selectedProject!["id"].rawString()!,"pageSize": "10000","curPage":"1"]
        case "UPLOAD":
            return [:]
        case "GETSERIALNUMBER":
            return [:]
        default:
            return ["pageSize":String(pagesize),"curPage":String(pickerpage)]
        }
    }
    
    override func files(for name: String) -> Array<Data> {
//        return fi
        return files.map({ (image,str) -> Data in
            return image.jpegData(compressionQuality: 0.2) ?? Data()
        })
    }
    
    override func method(for name: String) -> NetMethod {
        if name == "Submit" {
            return .post
        } else if name == "UPLOAD" {
            return .upload
        } else {
            return .get
        }
    }
    
    override func success(for name: String) -> NetSuccess {
        func projectListSuc(json: JSON)->Void{
            
            if let success = json.contents?["success"]?.bool {
                if success == true {
                    if let data = json.contents?["data"]?.contents?["list"]?.array {
                        self.projects = data
                        picker.reloadAllComponents()
                    }
                } else {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        func vechcles(json: JSON)->Void{
            if let _ = json.contents?["success"]?.bool {
                if let data = json.contents?["data"]?.array {
                    self.vechcles = data
                    self.vechcles.append(JSON(["name": "请选择","type":"cancel"]))
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        func mans(json: JSON)->Void{
            if let success = json.contents?["success"]?.bool {
                if success == true {
                    
                    if let data = json["data"].array {
                        self.mans = data
                        
                    }
                } else {
//                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
            } else {
//                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        func submit(json: JSON)->Void{
            if let success = json.contents?["success"]?.bool {
                print(json)
                if success == true {
                    if let previous = self.navigationController!.children[(self.navigationController!.children.count - 2)] as? ReimbursementListViewController {
                        previous.beginRefresh()
                    }
                    self.pop()
                } else {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        
        func upload(json: JSON)->Void {
            
            if json.contents?["success"]?.bool == true {
                let data = json.contents?["data"]?.contents
                var dic: Dictionary<String,String> = [:]
                dic["path"] = data?["path"]?.rawString() ?? ""
                dic["name"] = data?["name"]?.rawString() ?? ""
                dic["fileSize"] = String((data?["fileSize"]?.int ?? 0))
                dic["absolutePath"] = data?["absolutePath"]?.rawString() ?? ""
                uploadfilespath.append(dic)
                uploadsuccess += 1
                if uploadsuccess == files.count {
                    self.load(with: "Submit")
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
            
        }
        
        func getserialnum(json: JSON)->Void{
            if let success = json.contents?["success"]?.bool  {
                if success == true {
                    let data = json.contents?["data"]?.contents
                    if let number = data?["serialNumber"]?.rawString() {
                        self.serialNumber = number
                    }
                }
            }
        }
        
        if name == "Projects" {
            return projectListSuc
        }else if name == "TogetherMans" {
            return mans
        }else if name == "Vechcles"{
            return vechcles
        }else if name == "Submit"{
            return submit
        }else if name == "GETSERIALNUMBER" {
            return getserialnum
        }else{
            return upload
        }
    }
    
    var uploadsuccess = 0
    
    @objc func toApproval()->Void{
        uploadsuccess = 0
        self.view.endEditing(true)
        if self.files.count == 0 {
            self.load(with: "Submit")
        } else {
            self.load(with: "UPLOAD")
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 5
        } else if section < 1 + details.count {
            return 8
        } else if section == 1 + details.count{
            return 5
        } else {
            return files.count
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3 + details.count
    }

    func cell(with identifer: String,indexpath: IndexPath)->UITableViewCell{
        for (cell,index) in cells {
            if indexpath == index {
                return cell
            }
        }
        if identifer.contains("UITableViewCell") {
            let reuseIdentifier = identifer + String(format: "%2d-%2d", indexpath.section,indexpath.row)
            var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
            if cell == nil {
                cell = ParameTableViewCell.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
                cell?.textLabel?.textColor = UIColor.hex("333333")
                cell?.detailTextLabel?.textColor = UIColor.hex("999999")
                cell?.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
                cell?.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 15)
                cell?.selectionStyle = .none
                cells.append((cell!,indexpath))
                return cell!
            } else {
                cells.append((cell!,indexpath))
                return cell!
            }
        } else if identifer.contains("InputTableViewCell") {
            let reuseIdentifier = identifer + String(format: "%02d-%02d", indexpath.section,indexpath.row)
            var cell = self.tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as? InputTableViewCell
            if cell == nil {
                cell = InputTableViewCell.init(style: .default, reuseIdentifier: reuseIdentifier)
                cell!.selectionStyle = .none
                cell!.contentLabel.delegate = self
                
            }
            cells.append((cell!,indexpath))
            return cell!
        }
        
        return UITableViewCell()
    }
    
    var cells: [(UITableViewCell,IndexPath)] = []
    
    func cellForRow(at indexPath: IndexPath, identifier: String)->UITableViewCell? {
        for (cell,index) in cells {
            if index == indexPath {
                return cell
            }
        }
        let cell = self.cell(with: identifier, indexpath: indexPath)
        return cell
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            if indexPath.row == 0 {
                let cell = self.cell(with: "UITableViewCell",indexpath: indexPath) as! ParameTableViewCell
                cell.textLabel?.text = "项目名称:"
                cell.detailTextLabel?.text = selectedProject?.contents?["gsProjName"]?.rawString() ?? "请选择"
                cell.accessoryType = .disclosureIndicator
                return cell
            }
            else if indexPath.row == 1 {
                let cell = self.cell(with: "UITableViewCell",indexpath: indexPath) as! ParameTableViewCell
                cell.textLabel?.text = "同行人员:"
                cell.detailTextLabel?.text = members
                cell.detailTextLabel?.numberOfLines = 0
                cell.accessoryType = .disclosureIndicator
                return cell
            } else if indexPath.row == 2 {
                let cell = self.cell(with: "UITableViewCell",indexpath: indexPath) as! ParameTableViewCell
                cell.textLabel?.text = "出差人数:"
                cell.detailTextLabel?.text = memberscount
                return cell
            } else if indexPath.row == 3 {
                let cell = self.cell(with: "InputTableViewCell",indexpath: indexPath) as! InputTableViewCell
                cell.titleLbl.text = "出差天数:"
                cell.isnumber = true
                cell.contentLabel.keyboardType = .decimalPad
                return cell
            } else if indexPath.row == 4 {
                let cell = self.cell(with: "InputTableViewCell",indexpath: indexPath) as! InputTableViewCell
                cell.titleLbl.text = "出差事由:"
                return cell
            }
            return UITableViewCell()
        } else if (indexPath.section < 1 + details.count) {
            if (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 5 || indexPath.row == 6) {
                let cell = self.cell(with: "InputTableViewCell",indexpath: indexPath) as! InputTableViewCell
                if indexPath.row == 2 {
                    cell.titleLbl.text = "出发地点:"
                }else if (indexPath.row == 5){
                    cell.titleLbl?.text = "交通费(元)"
                    cell.contentLabel.keyboardType = .decimalPad
                }else if (indexPath.row == 6){
                    cell.titleLbl?.text = "住宿费(元):"
                    cell.contentLabel.keyboardType = .decimalPad
                }
                else {
                    cell.titleLbl.text = "到达地点:"
                }
                return cell
            } else {
                let cell = self.cell(with: "UITableViewCell",indexpath: indexPath)
                if indexPath.row == 0 {
                    cell.textLabel?.text = "出发日期:"
                    cell.detailTextLabel?.text = cell.detailTextLabel?.text ?? "请选择"
                } else if (indexPath.row == 1) {
                    cell.textLabel?.text = "到达日期:"
                    cell.detailTextLabel?.text = cell.detailTextLabel?.text ?? "请选择"
                }else if (indexPath.row == 4) {
                    cell.textLabel?.text = "交通方式:"
                    cell.detailTextLabel?.text = cell.detailTextLabel?.text ?? "请选择"
                }else{
                    cell.textLabel?.text = "是否超标:"
                    cell.detailTextLabel?.text = cell.detailTextLabel?.text ?? "请选择"
                }
                return cell
            }
        } else if indexPath.section == 1 + details.count {
            if (indexPath.row == 0) {
                if let cell = self.cell(with: "InputTableViewCell",indexpath: indexPath) as? InputTableViewCell {
                    cell.titleLbl.text = "交通补助(元):"
                    cell.contentLabel.keyboardType = .decimalPad
                    return cell
                }
                
                return self.cell(with: "InputTableViewCell", indexpath: indexPath)
            } else if (indexPath.row == 1) {
                let cell = self.cell(with: "InputTableViewCell",indexpath: indexPath) as! InputTableViewCell
                cell.titleLbl.text = "伙食补助(元):"
                cell.contentLabel.keyboardType = .decimalPad
                return cell
            } else if (indexPath.row == 2) {
                let cell = self.cell(with: "UITableViewCell",indexpath: indexPath) as! ParameTableViewCell
                cell.textLabel?.text = "费用合计(元):"
                cell.detailTextLabel?.text = fyhj
                return cell
            } else if (indexPath.row == 3) {
                let cell = self.cell(with: "UITableViewCell",indexpath: indexPath) as! ParameTableViewCell
                cell.accessoryType = .none
                cell.textLabel?.text = "费用合计大写:"
                cell.detailTextLabel?.text = fyhjdx
                return cell
            } else if (indexPath.row == 4){
                let cell = self.cell(with: "InputTableViewCell",indexpath: indexPath) as! InputTableViewCell
                cell.titleLbl.text = "备注:"
                return cell
            }
        } else {
            var cell = tableView.dequeueReusableCell(withIdentifier: "FileCell")
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "FileCell")
                cell?.accessoryType = .none
                let delbtn = UIButton.init(type: .custom)
                delbtn.addTarget(self, action: #selector(deltefile(button:)), for: .touchUpInside)
                delbtn.setImage(UIImage(named: "icon_cancel"), for: .normal)
                delbtn.tag = 10000 + indexPath.row
                cell?.contentView.addSubview(delbtn)
                delbtn.frame = CGRect.init(origin: CGPoint.init(x: tableView.width - 30, y: 22.5), size: CGSize(width: 15, height: 15))
                cell?.textLabel?.text = files[indexPath.row].1
                cell?.imageView?.image = UIImage.init(named: "image")
                cell?.imageView?.contentMode = .scaleAspectFill
                cell?.imageView?.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 30, height: 30))
                cell?.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
                cell?.textLabel?.textColor = UIColor.hex("333333")
                cell?.selectionStyle = .none
            }
            return cell!
        }
        return UITableViewCell()
    }
    
    @objc func deltefile(button: UIButton)->Void{
        let index = button.tag - 10000
        files.remove(at: index)
        self.tableView.beginUpdates()
        self.tableView.deleteRows(at: [IndexPath.init(row: index, section: details.count + 2)], with: .automatic)
        self.tableView.endUpdates()
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        if section < details.count {
            return 0
        }
        
        if section == details.count {
            return 40
        }
        
        if section == details.count + 2 {
            return 40
        }
        
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if (section < details.count ) {return nil}
        if ( section == details.count ) {
            let view = UIView()
            let line = UIView()
            line.backgroundColor = tableView.separatorColor
            line.frame = CGRect(origin: CGPoint.init(x: 15, y: 0), size: CGSize(width: UIScreen.main.bounds.size.width - 15, height: 0.5))
            view.addSubview(line)
            view.backgroundColor = UIColor.white
            let button = UIButton.init(type: .system)
            button.backgroundColor = UIColor.white
            button.setTitle("+增加报销明细", for: .normal)
            button.setTitleColor(UIColor.mainColor, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 200, height: 39.5))
            button.left = UIScreen.main.bounds.size.width / 2 - 100
            button.top = 0.5;
            view.addSubview(button)
            button.addTarget(self, action: #selector(addDetail), for: .touchUpInside)
            return view
        } else if (section == details.count + 2) {
            let view = UIStackView()
            view.alignment = .center
            view.axis = .horizontal
            view.distribution = .fillEqually
            let button = UIButton.init(type: .custom)
            button.setTitle("新增附件", for: .normal)
            button.setTitleColor(UIColor.mainColor, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.size = CGSize(width: 100, height: 30)
            button.addTarget(self, action: #selector(popaddfile), for: .touchUpInside)
            view.addArrangedSubview(button)
            return view
            
        }
        
        return nil
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.approvalBtn.isHidden = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.approvalBtn.isHidden = true
    }
    
    @objc func popaddfile()->Void{
        
        let alert = UIAlertController.init(title: "选取附件", message: nil, preferredStyle: .actionSheet)
        
        let photo = UIAlertAction.init(title: "照片", style: .default) { (action) in
            
            if 9 - self.files.count > 0 {
                if let picker = TZImagePickerController.init(maxImagesCount: 9 - self.files.count, delegate: self) {
                    
                    func pick(images: Array<UIImage>?, assets: Array<Any>?,isselectorigin: Bool) -> Void {
                        guard images != nil else {
                            self.alert("获取图片失败")
                            return
                        }
                        guard assets != nil else {
                            self.alert("获取图片失败")
                            return
                        }
                        for i in 0 ..< images!.count {
                            let image = images![i]
                            let asset = assets![i] as! PHAsset
                            if asset.value(forKey: "filename") != nil {
                                self.files.append((image,asset.value(forKey: "filename")! as! String))
                            } else {
                                self.files.append((image,String(i) + ".png"))
                            }
                        }
                        let set = IndexSet.init(integer: self.details.count + 2)
                        self.tableView.beginUpdates()
                        self.tableView.reloadSections(set, with: .automatic)
                        self.tableView.endUpdates()
                    }
                    
                    picker.didFinishPickingPhotosHandle = pick
                    
                    self.present(picker, animated: true, completion: nil)
                }
            } else {
                self.alert("附件多做选9张")
            }
            
        }
        
        
        let cancel = UIAlertAction.init(title: "取消", style: .cancel) { (action) in
            
        }
        alert.addAction(photo)
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
    }
    
    func giveModel(json: JSON) {
        self.selectedProject = json
        self.parterners = []
        self.mans = []
        self.tableView.reloadData()
        self.load(with: "TogetherMans")
    }
    
    func giveModels(jsons: [JSON]) {
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 && indexPath.section == 0 {
            let vc = SearchProjectsViewController()
            vc.title = "项目选择"
            vc.delegate = self
            self.push(destination: vc)
        }else if indexPath.row == 4 && indexPath.section == 0 {
            return
        }else if indexPath.row == 1 && indexPath.section == 0 {
            if self.mans.count == 0 {
                alert("同行人员为空，请重新选择项目")
                return
            }
            let dest = ParternersTableViewController()
            dest.title = "同行人员选择"
            dest.source = self.mans.filter({ (json) -> Bool in
                return json["account"].string != User.account
            })
            dest.selectedIndex = self.parterners.map({ (json) -> Int in
                return self.mans.firstIndex(of: json) ?? -1
            })
            dest.delegate = self
            self.push(destination: dest)//进入选择同行人员的界面
        } else if indexPath.row == 0 && indexPath.section < 1 + details.count {
            beginTimeCurrentIndex = indexPath
            let cell = cellForRow(at: IndexPath.init(row: indexPath.row + 1, section: indexPath.section), identifier: "UITableViewCell")
            let datestr = cell?.detailTextLabel?.text ?? ""
            let format = DateFormatter()
            format.dateFormat = "yyyy-MM-dd"
            if let date = format.date(from: datestr) {
                timePicker.maximumDate = date.addingTimeInterval(TimeInterval(8 * 3600))
            } else {
                timePicker.maximumDate = nil
                timePicker.minimumDate = nil
            }
            timeInput.becomeFirstResponder()//选择开始日期
        } else if indexPath.row == 1 && indexPath.section < 1 + details.count {
            endTimeCurrentIndex = indexPath//开始选择结束日期
            let cell = cellForRow(at: IndexPath.init(row: indexPath.row - 1, section: indexPath.section), identifier: "UITableViewCell")
            let datestr = cell?.detailTextLabel?.text ?? ""
            let format = DateFormatter()
            format.dateFormat = "yyyy-MM-dd"
            if let date = format.date(from: datestr) {
                timePicker.minimumDate = date.addingTimeInterval(TimeInterval(8 * 3600))
            } else {
                timePicker.maximumDate = nil
                timePicker.minimumDate = nil
            }
            timeInput.becomeFirstResponder()
        } else if indexPath.row == 4 && indexPath.section < 1 + details.count {
            currentVechicleIndex = indexPath
            projectorvehicleordb = (false,true,false)
            projectInput.becomeFirstResponder()
        } else if indexPath.row == 7 && indexPath.section < 1 + details.count {
            currentdbIndex = indexPath
            projectorvehicleordb = (false,false,true)
            projectInput.becomeFirstResponder()
        } else if indexPath.section == 2 + details.count {
            var models: [MSSBrowseModel] = []
            for i in images {
                let model = MSSBrowseModel.init()
                if let cell = self.tableView.cellForRow(at: indexPath) {
                    model.bigImage = i
                    model.smallImageView = cell.imageView
                    models.append(model)
                }
            }
            let mb = MSSBrowseLocalViewController.init(browseItemArray: models, currentIndex: indexPath.row)
            mb?.showBrowseViewController()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0
        } else if section < 1 + details.count {
            return 40
        } else if section == 1 + details.count{
            return 10
        } else {
            return 40
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section < 1 + details.count {
            let view = UIView()
            view.backgroundColor = UIColor.hex("eeeeee")
            let label = UILabel()
            label.text = "报销明细" + "(" + String(section) + ")"
            label.textColor = UIColor.hex("333333")
            label.font = UIFont.systemFont(ofSize: 15)
            label.width = label.text!.size(with: label.font).width
            label.height = label.text!.size(with: label.font).height
            label.top = 20 - 0.5 * label.height
            label.left = 15
            view.addSubview(label)
            let button = UIButton.init(type: .system)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.setTitleColor(UIColor.mainColor, for: .normal)
            button.setTitle("删除", for: .normal)
            button.frame = CGRect.init(origin: CGPoint(x: self.view.width - 65, y: 5), size: CGSize.init(width: 50, height: 30))
            if details.count != 1 {
                view.addSubview(button)
                button.tag = section - 1;
                button.addTarget(self, action: #selector(dlt(button:)), for: .touchUpInside)
            }
            return view
        }else if section == 2 + details.count {
            
            let footer = UIView()
            
            footer.backgroundColor = UIColor.dftBack
            
            footer.backgroundColor = UIColor.white
            
            let titleLbl = UILabel()
            
            titleLbl.textColor = UIColor.textColor
            
            titleLbl.font = UIFont.boldSystemFont(ofSize: 18)
            
            titleLbl.text = "附件:"
            
            titleLbl.frame = CGRect(origin: CGPoint(x: 15.0, y: 15.0), size: CGSize(width: titleLbl.contentSize.width, height: titleLbl.contentSize.height))
            
            footer.addSubview(titleLbl)
            
            return footer
        }else {
            return nil
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if indexPath.row == 1 || indexPath.row == 0 {
                let str = members
                let onewordheight = "我".size(with: UIFont.systemFont(ofSize: 15))
                let size = str.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: UIScreen.main.bounds.size.width - 45, height: 10000))
                return 60 + size.height - onewordheight.height
            } else {
                return 60
            }
        } else if indexPath.section < 1 + details.count {
            return 60
        } else {
            return 60
        }
    }
    
    @objc func addDetail()->Void{
       
        var deltposition: [(UITableViewCell,IndexPath)] = []
        for i in 0 ..< cells.count {
            let item = cells[i]
            if item.1.section > details.count {
                deltposition.append(item)
            }
        }
        
        let needappends = deltposition.map { (cell,index) -> (UITableViewCell,IndexPath) in
            return (cell,IndexPath.init(row: index.row, section: index.section + 1))
        }
        
        cells = cells.filter { (cell,index) -> Bool in
            return index.section <= details.count
        }
        
        cells.append(contentsOf: needappends)
        
        self.details.append(JSON())
        self.selectedVechicles.append(JSON())
        self.selectedDB.append(JSON())
        self.tableView.beginUpdates()
        let set = IndexSet.init(integer:  self.details.count)
        self.tableView.insertSections(set, with: .automatic)
        self.tableView.endUpdates()
        self.tableView.reloadData()
//        let sections = IndexSet.init(integersIn: 1 ... self.details.count)
//        self.tableView.reloadSections(sections, with: .none)
//        self.tableView.reload
    }
    
    @objc func dlt(button: UIButton)->Void{
        
        if self.details.count > button.tag {
            self.details.remove(at: button.tag)
        }
        
        //tag = section - 1
        
        //tag 从0 开始
        let needchangeorigin = cells.filter { (cell,index) -> Bool in
            return index.section > button.tag + 1
        } //原有的应该修改的cell集合 没错
//        for i in needchangeorigin  {
//            print(i.1.section)
//        }
        
        let needchange = needchangeorigin.map { (cell,index) -> (UITableViewCell,IndexPath) in
            return (cell,IndexPath.init(row: index.row, section: index.section - 1))
        } //修改好了的cell集合
        
//        for i in needchange  {
//            print(i.1.section)
//
//        }
        
        //section = 1 cells0
        cells = cells.filter { (cell,index) -> Bool in
                return index.section < button.tag + 1
        }//需要把选中的cell与cell后面的cell都删除
        for i in cells  {
            print(i.1.section)
            
        }
        
        cells.append(contentsOf: needchange)
        
        if self.selectedVechicles.count > button.tag {
            self.selectedVechicles.remove(at: button.tag)
        }
        
        if self.selectedDB.count > button.tag {
            self.selectedDB.remove(at: button.tag)
        }
        self.tableView.beginUpdates()
        let set = IndexSet.init(integer: button.tag + 1)
        self.tableView.deleteSections(set, with: .fade)
        self.tableView.endUpdates()
        let sections = IndexSet.init(integersIn: 1 ... self.details.count)
        self.tableView.reloadSections(sections, with: .none)
        
    }
    var beginTimeCurrentIndex: IndexPath? //当前开始日期所在的Indexpath
    var endTimeCurrentIndex: IndexPath? //当前到达日期所在的IndexPath
    var currentVechicleIndex: IndexPath? //当前选择交通方式所在的Index
    var currentdbIndex: IndexPath? //当前是否达标所在的Index
    
    let tableView = UITableView.init(frame: CGRect.zero, style: .plain)

    
  
    var projectId: String {
        get{
            return selectedProject?["id"].rawString() ?? ""
        }
    }
    
    var projectName: String {
        
        get{
            
            return selectedProject?["gsProjName"].rawString() ?? "请选择"
            
        }
    }
    
    var membersid: String {
        get{
            return parterners.map({ (json) -> String in
                return json["id"].rawString() ?? ""
            }).joined(separator: ",")
        }
    }
    
    var days: String {
        get{
            let cell = self.cellForRow(at: IndexPath.init(row: 3, section: 0), identifier: "InputTableViewCell") as! InputTableViewCell
            let daystr =  cell.contentLabel.text ?? ""
            return daystr
        }
    }
    
    var reasons: String{
        get{
            let cell = self.cellForRow(at: IndexPath.init(row: 4, section: 0), identifier: "InputTableViewCell") as! InputTableViewCell
            return cell.contentLabel.text ?? ""
        }
    }
    
    var uploaddetails: [Dictionary<String,String>] {
        get{
            return (1 ..< 1 + details.count).map({ (section) -> Dictionary<String,String> in
                //reimbursementDetails
                var dictionary: Dictionary<String,String> = [:]
                let cell = self.cellForRow(at: IndexPath.init(row: 0, section: section), identifier: "UITableViewCell")!
                dictionary["departureTime"] = cell.detailTextLabel?.text ?? ""
                let cell1 = self.cellForRow(at: IndexPath.init(row: 1, section: section), identifier: "UITableViewCell")!
                dictionary["arriveTime"] = cell1.detailTextLabel?.text ?? ""
                let cell2 = self.cellForRow(at: IndexPath.init(row: 2, section: section), identifier: "InputTableViewCell") as! InputTableViewCell
                dictionary["departurePlace"] = cell2.contentLabel.text ?? ""
                let cell3 = self.cellForRow(at: IndexPath.init(row: 3, section: section), identifier: "InputTableViewCell") as! InputTableViewCell
                dictionary["arrivalPlace"] = cell3.contentLabel.text ?? ""
                let cell4 = self.cellForRow(at: IndexPath.init(row: 4, section: section), identifier: "UITableViewCell")
                let typename = cell4?.detailTextLabel?.text
                if typename != "请选择" && (typename?.count ?? 0) > 0 {
                    vechcles.forEach({ (json) in
                        if json.contents?["name"]?.rawString() == typename {
                            dictionary["transportModes"] = json.contents?["type"]?.rawString() ?? ""
                        }
                    })
                }
                let cell5 = self.cellForRow(at: IndexPath.init(row: 5, section: section), identifier: "InputTableViewCell") as! InputTableViewCell

                if let transportationCost = Double.init(cell5.contentLabel.text ?? "0") {
                    dictionary["transportationCost"] = String(Int(transportationCost * 100))
                } else {
                    dictionary["transportationCost"] = "0"
                }
//
                let cell6 = self.cellForRow(at: IndexPath.init(row: 6, section: section), identifier: "InputTableViewCell") as! InputTableViewCell
                if let hotelExpense = Double.init(cell6.contentLabel.text ?? "0"){
                    dictionary["hotelExpense"] = String(Int(hotelExpense * 100))
                } else {
                    dictionary["hotelExpense"] = String(0)
                }
//                dictionary["hotelExpense"] = cell6.contentLabel.text ?? ""
                let cell7 = self.cellForRow(at: IndexPath.init(row: 7, section: section), identifier: "UITableViewCell")
                let ifdb = cell7?.detailTextLabel?.text
                if ifdb == "是" {
                    dictionary["whetherOverStand"] = "true"
                } else {
                    dictionary["whetherOverStand"] = "false"
                }
                return dictionary
            })
        }
    }
    
    //交通补助
    var jtbz: String {
        get{
            if let cell = self.cellForRow(at: IndexPath.init(row: 0, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell {
                if let value = Double.init(cell.contentLabel.text ?? "0") {
                     return String(Int(value * 100))
                }
            }
            return ""
        }
    }
    
    var hsbz: String {
        get{
            if let cell = self.cellForRow(at: IndexPath.init(row: 1, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell {
                if let value = Double.init(cell.contentLabel.text ?? "0") {
                    return String(Int(value * 100))
                }
            }
            return ""
            
        }
    }
    
    var total: Int{
        get{
            if let inthsbz = Double(hsbz) {
                if let intjtbz = Double(jtbz) {
                    return Int(inthsbz) + Int(intjtbz)
                }
            }
            return 0
        }
    }
    
    var remarks: String {
        get{
            if let cell = self.cellForRow(at: IndexPath.init(row: 4, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell {
                return cell.contentLabel.text ?? ""
            }
            return ""
        }
    }
 //要上传的参数
    
    var fyhj: String {
        get{
            let count = details.count
            var total = 0.00
            for section in 1 ..< 1 + count {
                if let cell1 = self.cellForRow(at: IndexPath.init(row: 5, section: section), identifier: "InputTableViewCell") as? InputTableViewCell {
                    if let val = Double.init(cell1.contentLabel.text ?? "0") {
                        total += val
                    }
                }
                if let cell2 = self.cellForRow(at: IndexPath.init(row: 6, section: section), identifier: "InputTableViewCell") as? InputTableViewCell {
                    if let val1 = Double.init(cell2.contentLabel.text ?? "0") {
                        total += val1
                    }
                }
            }
            
            var jtbzd = 0.0
            
            if let cell = self.cellForRow(at: IndexPath.init(row: 0, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell {
                if let value = Double.init(cell.contentLabel.text ?? "0") {
                    jtbzd = value
                }
            }
            
            var hsbzd = 0.0
            if let cell = self.cellForRow(at: IndexPath.init(row: 1, section: 1 + details.count), identifier: "InputTableViewCell") as? InputTableViewCell {
                if let value = Double.init(cell.contentLabel.text ?? "0") {
                    hsbzd = value
                }
            }
            total = total + jtbzd + hsbzd
            return String(format: "%.2f", total)
        }
    }
    
    var fyhjdx : String {
        get{
            return fyhj.rmbOldStyle
        }
    }
    
    var members: String {
        get{
            guard parterners.count > 0 else {
                return ""
            }
            let names = parterners.map { (json) -> String in
                return json["name"].rawString() ?? ""
            }
            return names.joined(separator: ",")
        }
    }
    
    var memberscount: String {
        get{
            return String(parterners.count + 1)
        }
    }
    
    var projects: [JSON] = []
    
    var vechcles: [JSON] = []
    
    var mans: [JSON] = []
    
    var selectedProject: JSON?
    
    var parterners: [JSON] = []
    
    var selectedParterner: JSON = []
    
    var files: [(UIImage,String)] = []
    
    var images: [UIImage] {
        get{
            return files.map({ (image,str) -> UIImage in
                return image
            })
        }
    }
    
    var details: [JSON] = [JSON()]
    
    var selectedVechicles: [JSON] = [JSON()]
    
    var selectedDB: [JSON] = []
    
    var projectorvehicleordb : (Bool,Bool,Bool) = (false,false,false)
    
    let approvalBtn = UIButton.init(type: .system)
    
    let picker = UIPickerView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 220)))
    
    let timeInput = UITextField()
    
    let timePicker = UIDatePicker.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 220)))
    
    let projectInput = UITextField()
    
    let vechicle = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(projectInput)
        self.view.addSubview(vechicle)
        
        tableView.delegate = self
        self.navigationController?.navigationBar.tintColor = UIColor.white
        
        tableView.dataSource = self
        
        self.view.addSubview(tableView)
        
        approvalBtn.setTitle("提交申请", for: .normal)
        
        approvalBtn.setTitleColor(UIColor.white, for: .normal)
        
        approvalBtn.setTitleColor(UIColor.hex("999999"), for: .disabled)
        
        approvalBtn.backgroundColor = UIColor.mainColor
        
        approvalBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        approvalBtn.addTarget(self, action: #selector(toApproval), for: .touchUpInside)
        
        self.view.addSubview(approvalBtn)
        
        self.load(with: self.names[0])
        
        self.load(with: self.names[1])
        
        self.load(with: self.names.last!)
        
        self.tableView.register(UINib.init(nibName: "InputTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "InputTableViewCell")
        
        self.picker.delegate = self
        
        self.picker.dataSource = self
        
        self.picker.backgroundColor = UIColor.white
        
        projectInput.inputView = self.picker
        
        vechicle.inputView = self.picker
        
        self.timePicker.backgroundColor = UIColor.white
        
        let locale = Locale.init(identifier: "zh_CN")
        
        self.timePicker.locale = locale
        
        self.timePicker.datePickerMode = .date
        
        timeInput.inputView = self.timePicker
        
        self.view.addSubview(timeInput)
        
        NotificationCenter.default.addObserver(self, selector: #selector(selectitem), name: UIResponder.keyboardWillHideNotification, object: nil)
    
        NetManager.get("financesAccesory/mobile/business/travelReimbursement/getNumber", nil, suc: { (json) in
            
        }) { (error) in
            
        }
    
    }
    
    @objc func selectitem()->Void{
        if projectorvehicleordb.0 == false && projectorvehicleordb.1 == false && projectorvehicleordb.2 == false  {
            //选定了日期
            if (beginTimeCurrentIndex != nil) {
                let cell = cellForRow(at: beginTimeCurrentIndex!, identifier: "UITableViewCell")
                cell?.detailTextLabel?.text = timePicker.date.dateLineStr
                beginTimeCurrentIndex = nil
            } else if (endTimeCurrentIndex != nil) {
                let cell = cellForRow(at: endTimeCurrentIndex!, identifier: "UITableViewCell")
                cell?.detailTextLabel?.text = timePicker.date.dateLineStr
                endTimeCurrentIndex = nil
            }
        }
        
        if projectorvehicleordb.0 == true {
            projectorvehicleordb.0 = false
            let row = picker.selectedRow(inComponent: 0)
            if (row > -1) {
                if row < projects.count {
                    selectedProject = projects[row]
                    let cell = cellForRow(at: IndexPath.init(row: 0, section: 0), identifier: "UITableViewCell")
                    cell?.detailTextLabel?.text = selectedProject?["gsProjName"].rawString();
                    self.load(with: "TogetherMans")
                }
            }
        }
        if projectorvehicleordb.1 == true {
            projectorvehicleordb.1 = false
            let row = picker.selectedRow(inComponent: 0)
            if row > -1 {
                if row < vechcles.count {
                    let cell = cellForRow(at: currentVechicleIndex!, identifier: "UITableViewCell")
                    let json = vechcles[row]
                    cell?.detailTextLabel?.text = json.contents?["name"]?.rawString()
                }
            }
        }
        
        if projectorvehicleordb.2 == true {
            projectorvehicleordb.2 = false
            let row = picker.selectedRow(inComponent: 0)
            if row > -1 {
                if row < 2 {
                    if (currentdbIndex != nil) {
                        let cell = cellForRow(at: currentdbIndex!, identifier: "UITableViewCell")
                        if row == 0 {
                            cell?.detailTextLabel?.text = "否"
                        } else {
                            cell?.detailTextLabel?.text = "是"
                        }
                        currentdbIndex = nil
                    }
                }
            }
        }
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        approvalBtn.top = self.view.height - 55
        approvalBtn.height = 55
        approvalBtn.left = 0
        approvalBtn.width = UIScreen.main.bounds.size.width
        self.tableView.top = 0
        self.tableView.left = 0
        self.tableView.width = UIScreen.main.bounds.size.width
        self.tableView.height = self.view.height - 55
    }
}
